package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrAttrgroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.common.utils.PageUtils;
import com.atguigu.gulimall.common.utils.R;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author yangshen
 * @email yangshen@gmail.com
 * @date 2020-05-25 17:51:12
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Resource
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 属性分组关联多个基础属性(规格参数)
     * @param attrAttrgroupRelationVos 入参
     * @return 成功标志
     */
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrAttrgroupRelationVo> attrAttrgroupRelationVos){
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationVos);
        return R.ok();
    }

    /**
     * 获根据属性分组id - 查询所有基础属性（规格参数）
     * @param attrgroupId 属性分组id
     * @return 属性分组下的所有基础属性（规格参数）
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R relationAttr(@PathVariable("attrgroupId")Long attrgroupId){
        List<AttrEntity> list = attrService.getRelationAttrs(attrgroupId);
        return R.ok().put("data", list);
    }

    /**
     * 根据分类id获取下面的属性分组和基础属性
     * @param catelogId 分类id
     * @return 属性分组和基础属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){
        //1.查出当前分类下的所有属性分组
        //2.查出每个属性分组下的所有属性
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", attrGroupWithAttrsVos);
    }

    /**
     * (页面操作) 属性分组 - 关联 - 新建关联
     * 根据属性分组id - 查询 不属于 该属性分组的基础属性（规格参数）
     * @param attrgroupId 属性分组id
     * @return 属性分组下没有的所有基础属性（规格参数）
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R relationNoAttr(@PathVariable("attrgroupId")Long attrgroupId, @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttrs(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * (页面操作)属性分组 - 批量删除 - 不删除基础属性（规格参数）只删掉属性组与基础属性的关系 pms_attr_attrgroup_
     * @param attrAttrgroupRelationVos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationVo> attrAttrgroupRelationVos){
        attrGroupService.deleteRelation(attrAttrgroupRelationVos);
        return R.ok();
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
